#include"init.h"
#include"comm/boot_info.h"
#include"cpu/cpu.h"
#include"cpu/irq.h"
#include"dev/time.h"
#include"tools/klib.h"
#include"core/task.h"
#include"comm/cpu_instr.h"
#include"ipc/sem.h"
#include"core/memory.h"
#include"dev/console.h"
#include"dev/kbd.h"
#include"fs/fs.h"

static sem_t sem;

void kernel_init(boot_info_t* boot_info)
{   
    //初始化CPU
    cpu_init();
    //处理中断与异常
    irq_init();
    //日志初始化
    log_init();
    //控制窗口初始化
    //console_init();
    //内存控制初始化
    mermory_init(boot_info);
    //文件系统初始化
    fs_init();
    //设置定时器
    time_init();
    //进程管理初始化
    task_mananger_init();
    //键盘初始化
    //kbd_init();

}

void  move_to_first_task()
{
    task_t * cur = cur_task();

    tss_t* tss = &(cur->tss);

    __asm__ __volatile__(
        "push %[ss]\n\t"			// SS
        "push %[esp]\n\t"			// ESP
        "push %[eflags]\n\t"        // EFLAGS
        "push %[cs]\n\t"			// CS
        "push %[eip]\n\t"		    // ip
        "iret\n\t"::[ss]"r"(tss->ss),  [esp]"r"(tss->esp), [eflags]"r"(tss->eflags),
        [cs]"r"(tss->cs), [eip]"r"(tss->eip));
}


void init_main()
{    
    print_log("======================================================");
    print_log("Kernel is running....");
    print_log("Version: %s, name: %s", OS_VERSION, "tiny x86 os");
    print_log("======================================================");
    task_first_init();
    move_to_first_task();
}